package cn.gson.school.controller.jiuye;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import cn.gson.school.dao.jiaoxue.StudentDao;
import cn.gson.school.dao.jiuye.FileEntityDao;
import cn.gson.school.dao.jiuye.InterviewMaterialsEntityDao;
import cn.gson.school.dao.jiuye.InterviewRecordEntityDao;
import cn.gson.school.dao.jiuye.MultimediaEntityDao;
import cn.gson.school.dao.jiuye.OfferDao;
import cn.gson.school.dao.jiuye.RetestEntityDao;
import cn.gson.school.entity.jiaoxue.StudentEntity;
import cn.gson.school.entity.jiuye.FileEntity;
import cn.gson.school.entity.jiuye.InterviewMaterialsEntity;
import cn.gson.school.entity.jiuye.InterviewRecordEntity;
import cn.gson.school.entity.jiuye.MultimediaEntity;
import cn.gson.school.entity.jiuye.OfferEntity;
import cn.gson.school.entity.jiuye.RetestEntity;
import cn.gson.school.entity.xitong.ImageEntity;
import cn.gson.school.entity.xitong.UserEntity;
import cn.gson.school.mappers.jiuye.InterviewRecordEntityMapper;
import cn.gson.school.service.ImagesService;

/**
 * 查询面试记录
 * 
 * @author 华少
 *
 */
@Controller
public class QianTaiChaXunMainShiJiLu {
	@Autowired
	private InterviewRecordEntityMapper interviewRecordEntityMapper;
	@Autowired
	private InterviewRecordEntityDao interviewRecordEntityDao;
	@Autowired
	private StudentDao studentDao;
	@Autowired
	private InterviewMaterialsEntityDao interviewMaterialsEntityDao;
	@Autowired
	private RetestEntityDao retestEntityDao;
	@Autowired
	private ImagesService imgService;
	@Autowired
	private OfferDao offerDao;
	@Autowired
	private FileEntityDao fileEntityDao;
	@Autowired
	private MultimediaEntityDao multimediaEntityDao;

	@GetMapping({ "chaxunjilu" })
	public String select(Model model, HttpServletRequest request) {
		try {
			String ids = request.getParameter("id");
			Long id = Long.valueOf(ids);
			if (id != 0) {
				List<InterviewRecordEntity> list = interviewRecordEntityMapper.chaxunwode(id);
				System.out.println(list);
				model.addAttribute("list", list);
				

			} else {
				List<InterviewRecordEntity> list = interviewRecordEntityMapper.chaxun();
				System.out.println(list);
				model.addAttribute("list", list);
			}
			return "qt-jy-cxjilu";
		} catch (Exception e) {
			return "qt-jy-cxjilu";
		}
	}

	@PostMapping({ "jilutianjia" })
	@ResponseBody
	public void add(@SessionAttribute("user") UserEntity user, @RequestParam("file") MultipartFile file,
			@RequestParam("yinpin") MultipartFile yinpin, @RequestParam(name = "plan_detail") String wenti,
			@RequestParam(name = "support") String danan, @RequestParam(name = "id") String id,
			@RequestParam(name = "language") String leixing, Model model, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws ServletException, IOException, ParseException {
		String interviewId = request.getParameter("interviewId");
		String yinId = request.getParameter("yinId");
		String retestId = request.getParameter("retestId");
		String imageId = request.getParameter("imageId");
		String offerId = request.getParameter("offerId");
		String fileId = request.getParameter("fileId");
		System.out.println("id:" + id);
		System.out.println("interviewId:" + interviewId);
		System.out.println("yinId:" + yinId);
		System.out.println("retestId:" + retestId);
		System.out.println("imageId:" + imageId);
		System.out.println("offerId:" + offerId);
		System.out.println("fileId:" + fileId);
		System.out.println("wenti:" + wenti);
		System.out.println("support:" + danan);

		StudentEntity stu = (StudentEntity) session.getAttribute("userinfo");
		String gongsi = request.getParameter("gongsi");
		String mianshishijian = request.getParameter("manshishijian");
		String mianshididian = request.getParameter("mianshididian");
		String yvan = request.getParameter("yvan");
		InterviewRecordEntity interview = new InterviewRecordEntity();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		Timestamp a = Timestamp.valueOf(df.format(new Date()));
		// interviewId
		if (!interviewId.equals("")) {
			interview.setInterviewId(Long.valueOf(interviewId));
		}
		interview.setUploadTime(a);
		interview.setInterviewCompany(gongsi);
		interview.setInterviewPlan(yvan);
		interview.setInterviewAddress(mianshididian);
		interview.setInterviewSuccess(2);
		interview.setInterviewTime(df.parse(mianshishijian));
		interview.setStudentinfoId(stu);
		// 添加面试记录表
		interviewRecordEntityDao.save(interview);
		InterviewRecordEntity e = null;
		if (interviewId.equals("")) {
			List<InterviewRecordEntity> record = (List<InterviewRecordEntity>) interviewRecordEntityDao.findAll();
			for (int i = 0; i < record.size(); i++) {
				e = record.get(i);
			}
		} else {
			e = interviewRecordEntityDao.findOne(Long.valueOf(interviewId));
		}

		ImageEntity yin = null;
		if (yinpin.getSize() != 0) {
			try {
				yin = imgService.upload(user, "jiuyeyinpin", yinpin, yinId);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		// 添加音频
		if (null != yin && yinId.equals("")) {
			MultimediaEntity multimedia = new MultimediaEntity();
			multimedia.setInterviewId(e);
			multimedia.setMultimediaTime(new Date());
			multimedia.setTouXiang(yin);
			multimediaEntityDao.save(multimedia);
		}
		String[] interviewMaterialsId = id.split(",");
		String[] syweiti = wenti.split(",");
		String[] sydanan = danan.split(",");
		String[] syleixing = leixing.split(",");
		for (int i = 0; i < syweiti.length; i++) {
			InterviewMaterialsEntity materials = new InterviewMaterialsEntity();
			if (!interviewMaterialsId[i].equals("-1")) {
				materials.setInterviewMaterialsId(Long.valueOf(interviewMaterialsId[i]));
			}
			materials.setInterviewMaterialsAnswer(sydanan[i]);
			materials.setInterviewMaterialsExperience(0);
			materials.setInterviewMaterialsQuestion(syweiti[i]);
			materials.setInterviewMaterialsTime(new Date());
			materials.setInterviewQuestionType(syleixing[i]);
			materials.setInterviewId(e);
			interviewMaterialsEntityDao.save(materials);
			if (!interviewMaterialsId[i].equals("-1")) {
				materials.setInterviewMaterialsId(null);
			}
			System.out.println("asdasdiuqwykjhsad2");
		}
		String retestTime = request.getParameter("retestTime");
		String retestAddress = request.getParameter("retestAddress");
		String retestPlan = request.getParameter("retestPlan");
		String retestPlus = request.getParameter("retestPlus");
		RetestEntity retest = new RetestEntity();
		if (!retestId.equals("")) {
			retest.setRetestId(Long.valueOf(retestId));
		}
		if (!"".equals(retestTime)) {
			retest.setRetestTime(df.parse(retestTime));
		}
		retest.setRetestAddress(retestAddress);
		retest.setRetestPlan(retestPlan);
		retest.setRetestPlus(retestPlus);
		retest.setInterviewId(e);
		retestEntityDao.save(retest);
		ImageEntity img = null;

		if (file.getSize() != 0) {
			try {
				img = imgService.upload(user, "jiuye", file, imageId);
			} catch (Exception s) {
			}
		}
		int y = 0;
		int oo=0;
		System.out.println("img:"+img);
			String offerTime = request.getParameter("offerTime");
				List<InterviewRecordEntity> s = interviewRecordEntityDao
						.chaxunsyneirong(e.getStudentinfoId().getStuId());
				for (int i = 0; i < s.size(); i++) {
					if (s.get(i).getInterviewSuccess() == 1) {
						y = 1;
					}
				}
			OfferEntity of = null;
			if (imageId.equals("")) {
				if(img!=null) {
					OfferEntity offerEntity = new OfferEntity();
					if (!offerId.equals("")) {
						offerEntity.setOfferId(Long.valueOf(offerId));
					}
					if (!"".equals(offerTime)) {
						offerEntity.setOfferTime(df.parse(offerTime));
					}
					offerEntity.setInterviewId(e);
					offerEntity.setUploadTime(new Date());
					offerEntity.setTouXiang(img);
					offerDao.save(offerEntity);
					oo=1;
					List<OfferEntity> offer = (List<OfferEntity>) offerDao.findAll();
					for (int i = 0; i < offer.size(); i++) {
						of = offer.get(i);
					}
				}
			}
			if (y == 0) {
				// 试用薪水
				String probationSalary = request.getParameter("probationSalary");
				// 转正薪水
				String regularSalary = request.getParameter("regularSalary");
				// 试用期
				String plus = request.getParameter("Plus");
				// 就业时间
				String employmentTime = request.getParameter("employmentTime");
				System.out.println(" 试用薪水:"+probationSalary);
				System.out.println(" 转正薪水:"+regularSalary);
				System.out.println(" 试用期:"+plus);

				FileEntity fileEntity = new FileEntity();
				if (!fileId.equals("")) {
					fileEntity.setFileId(Long.valueOf(fileId));
				}
				System.out.println("就业时间:" + employmentTime);
				if (!"".equals(employmentTime)) {
					fileEntity.setEmploymentTime(df.parse(employmentTime));
				}
				fileEntity.setFileReason("已就业");
				if (imageId.equals("")) {
					fileEntity.setOfferId(of);
				}else {
					fileEntity.setOfferId(offerDao.findOne(Long.valueOf(offerId)));
				}
				if (!"".equals(probationSalary) || !"".equals(regularSalary) || !"".equals(plus)) {
					try {
						fileEntity.setProbationSalary(Double.valueOf(probationSalary));
					} catch (Exception e2) {
					}
					try {
						fileEntity.setRegularSalary(Double.valueOf(regularSalary));
					} catch (Exception e2) {
					}try {
						fileEntity.setRetestPlus(Integer.valueOf(plus));
					} catch (Exception e2) {
					}
					fileEntityDao.save(fileEntity);
					StudentEntity studentEntity=studentDao.findOne(stu.getStuId());
					studentEntity.setState("4");
					studentDao.save(studentEntity);
					e.setInterviewSuccess(1);
					interviewRecordEntityDao.save(e);
				}
				
			}
		String iddd=(String) session.getAttribute("interviewId");
		if(interviewId.equals("")) {
			response.sendRedirect("qt-jy-mianshi");
		}else {
			response.sendRedirect("chaxunjiluxiangqing?id="+iddd);
		}
		
	}

}
